//
//  Noise.swift
//  CatPomodoroTimer
//
//  Created by Xuri Li on 2024/8/3.
//

import Foundation
import Combine

class Noise: ObservableObject, Hashable, Codable, Identifiable {
    var id: Int
    var title: String
    var fileName: String
    var imageName: String
    @Published var isSelected: Bool = false

    enum CodingKeys: String, CodingKey {
        case id, title, fileName, imageName
    }

    init(id: Int, title: String, fileName: String, imageName: String, isSelected: Bool = false) {
        self.id = id
        self.title = title
        self.fileName = fileName
        self.imageName = imageName
        self.isSelected = isSelected
    }
    
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        id = try container.decode(Int.self, forKey: .id)
        title = try container.decode(String.self, forKey: .title)
        fileName = try container.decode(String.self, forKey: .fileName)
        imageName = try container.decode(String.self, forKey: .imageName)
        isSelected = false
    }
    
    static func == (lhs: Noise, rhs: Noise) -> Bool {
        return lhs.id == rhs.id
    }
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
}
